package com.jhy.study2.IO;


import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author: robert
 * @description: 压缩多个文件练习
 * @date: 2021/8/23 18:59
 * @return
 */
public class MultiFileZip1 {
    public static void main(String[] args) throws Exception {
        zipMulFile();

    }

    public static void zipMulFile() throws Exception {
        File file = new File("D://testIO");
        File destination = new File("D://multiZip1.zip");


        //1.准备流
        InputStream input = null;
        ZipOutputStream out = new ZipOutputStream( new FileOutputStream(destination));
        out.setComment("cpmmmmmmmmmm");

        //2.获取文件数组
        if(file.exists()){
            if(file.isDirectory()){
                File[] fs = file.listFiles();
                int temp = 0;
                for(int i=0; i<fs.length; i++){
                    File f = fs[i];
//                    input = new FileInputStream(new File(f.getPath()+f.getName()));  //错误的
                    if(f.isDirectory()){
                        continue;
                    }
                    input = new FileInputStream(f);
                    out.putNextEntry(new ZipEntry(f.getName()));
                    System.out.println("正在压缩" + f.getName());


                    while((temp = input.read()) != -1){
                        out.write(temp);
                    }


                    input.close();
                }
            }
        }

        out.close();
        System.out.println("多文件压缩完毕");








    }


}
